home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6984 / 6984.xpi / chrome / lazarus.jar / content / background-task.js < prev    next >
Text File  |  2009-11-24  |  1KB  |  46 lines

  1.  
  2. (function(self){
  3.  
  4.   /**
  5.   *  run a task in a background thread, 
  6.   *  call callback when complete, or on error
  7.   */
  8.   self.backgroundTask = function(func, callback){
  9.   
  10.     //un-fraking-believable, backgroundTasks are no good when Firefox is loading,
  11.     //they still prevent the UI from being initalized
  12.     //so we'll wrap them in a setTimeout as well.
  13.     setTimeout(function(){
  14.       //create a new background thread
  15.       var returnVal = null;
  16.       var completed = false;
  17.       var err = null;
  18.       
  19.       var threadObj = {
  20.         run: function(){
  21.           try {
  22.             returnVal = func();
  23.           }
  24.           catch(err){
  25.             Components.utils.reportError(err);
  26.             returnVal = false;
  27.           }
  28.           completed = true;
  29.         }
  30.       }
  31.       
  32.       var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).newThread(0);
  33.       thread.dispatch(threadObj, thread.DISPATCH_NORMAL);
  34.       
  35.       var currThread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
  36.       while (!completed){
  37.           currThread.processNextEvent(true);
  38.       }
  39.       
  40.       callback(returnVal);
  41.     
  42.     }, 1);
  43.   }
  44.  
  45.  
  46. })(Lazarus);